%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% COMPUTE DEMAND FOR DEFORESTATION

% "DEFORESTATION IN THE AMAZON:
% A UNIFIED FRAMEWORK FOR ESTIMATION AND POLICY ANALYSIS"

% by Eduardo Souza-Rodrigues

% This version: November 2018

% OBSERVATIONS: 
% 1. Run this program after running "dem_def_reg.m"
% 2. The data set has to be loaded before running this program.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% (A) COMPUTE DEMAND FOR DEFORESTATION 

% I. Prepare the Data

% Transfers/Taxes, US$/ha (OBS: US$ 1 = R$ 2.17 in 2006)
transfer1 = 5:2.5:190;
transfer  = [0 transfer1 200 2000 10000];
clear transfer1


% II. Compute the Demand Function

% Fix Transfers
for p = 1:size(transfer,2)
    
    % Fix Observation (= Municipality)
    for j = 1:T
                       
        % (a) 2SLS        
        % Single-Index
        if penalty == 0     % Actual Monitoring (Main Specification)            
            
            SI_iv(j,p) = Xc(j,:) * beta_iv + Uiv(j,1) + beta_iv(1) * (transfer(p)/Qa(j,1));            
            
        elseif penalty == 1 % Fines post-2004 at pre-2004 level (i.e., Fines 2004-05 = Fines 2003-04)
            
            SI_iv(j,p) = Xc(j,:) * beta_iv + Uiv(j,1) + beta_iv(1) * (transfer(p)/Qa(j,1))...
                - beta_iv(2) * Fines2005(j) + beta_iv(2) * 2 * Fines2003(j);
            
        end 
              
        % Fraction of Land-use
        S_iv(j,p) = (exp(SI_iv(j,p)) / (1 + exp(SI_iv(j,p))));
        
        % Demand for Deforestation
        Demand_iv(j,p) = S_iv(j,p) * Area(j,1);
        
                
        % (b) IVQR        
        % Single-Index
        if penalty == 0     % Actual Monitoring (Main Specification)            
            
            SI_ivqr(j,p)  = Xc(j,:) * beta_ivqr(:,Uivqr(j,1)) + beta_ivqr(1,Uivqr(j,1)) * (transfer(p)/Qa(j,1));
            
        elseif penalty == 1 % Fines post-2004 at pre-2004 level (i.e., Fines 2004-05 = Fines 2003-04)
            
            SI_ivqr(j,p)  = Xc(j,:) * beta_ivqr(:,Uivqr(j,1)) + beta_ivqr(1,Uivqr(j,1)) * (transfer(p)/Qa(j,1))...
                - beta_ivqr(1,Uivqr(j,1)) * Fines2005(j) + beta_ivqr(1,Uivqr(j,1)) * 2 * Fines2003(j);
        
        end
        
        % Share of Land-use
        S_ivqr(j,p) = (exp(SI_ivqr(j,p)) / (1 + exp(SI_ivqr(j,p))));
        
        % Demand for Deforestation
        Demand_ivqr(j,p) = S_ivqr(j,p) * Area(j,1);
                        
    end
    
    % Total Demand (sum across municipalities)
    tot_demand_iv(p)   = sum(Demand_iv(:,p));    
    tot_demand_ivqr(p) = sum(Demand_ivqr(:,p).*(beta_ivqr(1,Uivqr)<=0)');      
   
end


%% (B) SAVE RESULTS

% Save the Data to Combine with Other Farm Sizes

if prod_index == 1  % Only Crops Index
    
    if farm == 1
           
        % Total Demand Function - 2SLS & IVQR
        Demand_farm_1c = [transfer' tot_demand_iv' tot_demand_ivqr'];

        % Demand for Deforestation for each observation - 2SLS
        deforest_farm_1c_iv = [Code Area Demand_iv];

        % Demand for Deforestation for each observation - IVQR
        deforest_farm_1c = [Code Area Demand_ivqr];

        % Save All
        save Demand_farm_1c        Demand_farm_1c        -ascii
        save deforest_farm_1c_iv   deforest_farm_1c_iv   -ascii
        save deforest_farm_1c      deforest_farm_1c      -ascii
        
    elseif farm == 2

        % Total Demand Function - 2SLS & IVQR
        Demand_farm_2c = [transfer' tot_demand_iv' tot_demand_ivqr'];

        % Demand for Deforestation for each observation - 2SLS
        deforest_farm_2c_iv = [Code Area Demand_iv];

        % Demand for Deforestation for each observation - IVQR
        deforest_farm_2c = [Code Area Demand_ivqr];

        % Save All
        save Demand_farm_2c        Demand_farm_2c        -ascii
        save deforest_farm_2c_iv   deforest_farm_2c_iv   -ascii
        save deforest_farm_2c      deforest_farm_2c      -ascii

    elseif farm == 3

        % Total Demand Function - 2SLS & IVQR
        Demand_farm_3c = [transfer' tot_demand_iv' tot_demand_ivqr'];

        % Demand for Deforestation for each observation - 2SLS
        deforest_farm_3c_iv = [Code Area Demand_iv];

        % Demand for Deforestation for each observation - IVQR
        deforest_farm_3c = [Code Area Demand_ivqr];

        % Save All
        save Demand_farm_3c        Demand_farm_3c        -ascii
        save deforest_farm_3c_iv   deforest_farm_3c_iv   -ascii
        save deforest_farm_3c      deforest_farm_3c      -ascii

    elseif farm == 4

        % Total Demand Function - 2SLS & IVQR
        Demand_farm_4c = [transfer' tot_demand_iv' tot_demand_ivqr'];

        % Demand for Deforestation for each observation - 2SLS
        deforest_farm_4c_iv = [Code Area Demand_iv];

        % Demand for Deforestation for each observation - IVQR
        deforest_farm_4c = [Code Area Demand_ivqr];

        % Save All
        save Demand_farm_4c        Demand_farm_4c        -ascii
        save deforest_farm_4c_iv   deforest_farm_4c_iv   -ascii
        save deforest_farm_4c      deforest_farm_4c      -ascii

    end
    
elseif prod_index == 2  % Crops-Pasture Index
   
    if farm == 1

        if penalty == 0     % Actual Monitoring (Main Specification)   

            % Total Demand Function - 2SLS & IVQR
            Demand_farm_1cp = [transfer' tot_demand_iv' tot_demand_ivqr'];

            % Demand for Deforestation for each observation - 2SLS
            deforest_farm_1cp_iv = [Code Area Demand_iv];

            % Demand for Deforestation for each observation - IVQR
            deforest_farm_1cp = [Code Area Demand_ivqr];

            % Save All
            save Demand_farm_1cp        Demand_farm_1cp        -ascii
            save deforest_farm_1cp_iv   deforest_farm_1cp_iv   -ascii
            save deforest_farm_1cp      deforest_farm_1cp      -ascii

            % Save in ".txt" to Export
            %dlmwrite('deforest_farm_1cp.txt', deforest_farm_1cp,'precision', 7);

        elseif penalty == 1 % Fines post-2004 at pre-2004 level         

            % Total Demand Function - 2SLS & IVQR
            Demand_farm_1cp_fines = [transfer' tot_demand_iv' tot_demand_ivqr'];

            % Demand for Deforestation for each observation - 2SLS
            deforest_farm_1cp_iv_fines = [Code Area Demand_iv];

            % Demand for Deforestation for each observation - IVQR
            deforest_farm_1cp_fines = [Code Area Demand_ivqr];

            % Save All
            save Demand_farm_1cp_fines        Demand_farm_1cp_fines        -ascii
            save deforest_farm_1cp_iv_fines   deforest_farm_1cp_iv_fines   -ascii
            save deforest_farm_1cp_fines      deforest_farm_1cp_fines      -ascii

        end
                   
    elseif farm == 2

        if penalty == 0     % Actual Monitoring (Main Specification)   

            % Total Demand Function - 2SLS & IVQR
            Demand_farm_2cp = [transfer' tot_demand_iv' tot_demand_ivqr'];

            % Demand for Deforestation for each observation - 2SLS
            deforest_farm_2cp_iv = [Code Area Demand_iv];

            % Demand for Deforestation for each observation - IVQR
            deforest_farm_2cp = [Code Area Demand_ivqr];

            % Save All
            save Demand_farm_2cp        Demand_farm_2cp        -ascii
            save deforest_farm_2cp_iv   deforest_farm_2cp_iv   -ascii
            save deforest_farm_2cp      deforest_farm_2cp      -ascii

            % Save in ".txt" to Export
            %dlmwrite('deforest_farm_2cp.txt', deforest_farm_2cp,'precision', 7);

        elseif penalty == 1 % Fines post-2004 at pre-2004 level         

            % Total Demand Function - 2SLS & IVQR
            Demand_farm_2cp_fines = [transfer' tot_demand_iv' tot_demand_ivqr'];

            % Demand for Deforestation for each observation - 2SLS
            deforest_farm_2cp_iv_fines = [Code Area Demand_iv];

            % Demand for Deforestation for each observation - IVQR
            deforest_farm_2cp_fines = [Code Area Demand_ivqr];

            % Save All
            save Demand_farm_2cp_fines        Demand_farm_2cp_fines        -ascii
            save deforest_farm_2cp_iv_fines   deforest_farm_2cp_iv_fines   -ascii
            save deforest_farm_2cp_fines      deforest_farm_2cp_fines      -ascii

        end   

    elseif farm == 3

        if penalty == 0     % Actual Monitoring (Main Specification)   

            % Total Demand Function - 2SLS & IVQR
            Demand_farm_3cp = [transfer' tot_demand_iv' tot_demand_ivqr'];

            % Demand for Deforestation for each observation - 2SLS
            deforest_farm_3cp_iv = [Code Area Demand_iv];

            % Demand for Deforestation for each observation - IVQR
            deforest_farm_3cp = [Code Area Demand_ivqr];

            % Save All
            save Demand_farm_3cp        Demand_farm_3cp        -ascii
            save deforest_farm_3cp_iv   deforest_farm_3cp_iv   -ascii
            save deforest_farm_3cp      deforest_farm_3cp      -ascii

            % Save in ".txt" to Export
            %dlmwrite('deforest_farm_3cp.txt', deforest_farm_3cp,'precision', 7);

        elseif penalty == 1 % Fines post-2004 at pre-2004 level         

            % Total Demand Function - 2SLS & IVQR
            Demand_farm_3cp_fines = [transfer' tot_demand_iv' tot_demand_ivqr'];

            % Demand for Deforestation for each observation - 2SLS
            deforest_farm_3cp_iv_fines = [Code Area Demand_iv];

            % Demand for Deforestation for each observation - IVQR
            deforest_farm_3cp_fines = [Code Area Demand_ivqr];

            % Save All
            save Demand_farm_3cp_fines        Demand_farm_3cp_fines        -ascii
            save deforest_farm_3cp_iv_fines   deforest_farm_3cp_iv_fines   -ascii
            save deforest_farm_3cp_fines      deforest_farm_3cp_fines      -ascii

        end   

    elseif farm == 4

        if penalty == 0     % Actual Monitoring (Main Specification)   

            % Total Demand Function - 2SLS & IVQR
            Demand_farm_4cp = [transfer' tot_demand_iv' tot_demand_ivqr'];

            % Demand for Deforestation for each observation - 2SLS
            deforest_farm_4cp_iv = [Code Area Demand_iv];

            % Demand for Deforestation for each observation - IVQR
            deforest_farm_4cp = [Code Area Demand_ivqr];

            % Save All
            save Demand_farm_4cp        Demand_farm_4cp        -ascii
            save deforest_farm_4cp_iv   deforest_farm_4cp_iv   -ascii
            save deforest_farm_4cp      deforest_farm_4cp      -ascii

            % Save in ".txt" to Export
            %dlmwrite('deforest_farm_4cp.txt', deforest_farm_4cp,'precision', 7);

        elseif penalty == 1 % Fines post-2004 at pre-2004 level         

            % Total Demand Function - 2SLS & IVQR
            Demand_farm_4cp_fines = [transfer' tot_demand_iv' tot_demand_ivqr'];

            % Demand for Deforestation for each observation - 2SLS
            deforest_farm_4cp_iv_fines = [Code Area Demand_iv];

            % Demand for Deforestation for each observation - IVQR
            deforest_farm_4cp_fines = [Code Area Demand_ivqr];

            % Save All
            save Demand_farm_4cp_fines        Demand_farm_4cp_fines        -ascii
            save deforest_farm_4cp_iv_fines   deforest_farm_4cp_iv_fines   -ascii
            save deforest_farm_4cp_fines      deforest_farm_4cp_fines      -ascii

        end
        
    end  % End "Farm Sizes"
    
end  % End "Prod_Index"



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% DELETE IF NOT NEEDED

clear transfer tc p j

clear SI_iv   S_iv   Demand_iv   
clear SI_ivqr S_ivqr Demand_ivqr 
clear tot_demand_iv  tot_demand_ivqr

